package com.shanghai.shanghaielectric.entity;

import android.util.Log;

import com.shanghai.shanghaielectric.Constants;
import com.shanghai.shanghaielectric.Model.ReceiveDataModel;
import com.shanghai.shanghaielectric.R;
import com.shanghai.shanghaielectric.controller.ParameterSetController.PassiveComponentType;
import com.shanghai.shanghaielectric.utils.CommUtil;

import java.util.ArrayList;
import java.util.List;

import shanghaielectric_lib.Utils.dwin.BitUtils;

/**
 * 用来记录电容投切状态    刷新主界面
 * 根据电容的投切状态，选择对应的图形进行显示
 *
 * @author ZDD
 */
public class CapacitorEntity {

    public int mPassiveType;//取值为1,2,3；     1:KVAR_240; 2:KVAR_300; 3:KVAR_360;

    public boolean isOpen = true;//电容通断状态

    public int firstAuxiliary;//4个共补状态
    public int secondAuxiliary;
    public int thirdAuxiliary;
    public int fourthAuxiliary;

    public int firstASub;//以下是9个分补状态
    public int firstBSub;
    public int firstCSub;

    public int secondASub;
    public int secondBSub;
    public int secondCSub;

    public int thirdASub;
    public int thirdBSub;
    public int thirdCSub;

    public String getSendHexString() {
        String data = "000";
        data = data + String.valueOf(thirdCSub);
        data = data + String.valueOf(thirdBSub);
        data = data + String.valueOf(thirdASub);

        data = data + String.valueOf(secondCSub);
        data = data + String.valueOf(secondBSub);
        data = data + String.valueOf(secondASub);

        data = data + String.valueOf(firstCSub);
        data = data + String.valueOf(firstBSub);
        data = data + String.valueOf(firstASub);

        data = data + String.valueOf(fourthAuxiliary);
        data = data + String.valueOf(thirdAuxiliary);
        data = data + String.valueOf(secondAuxiliary);
        data = data + String.valueOf(firstAuxiliary);
        android.util.Log.e("cbcbcbbc", "" + data);
        android.util.Log.e("cbcbcbbc", "" + CommUtil.binaryString2hexString(data));
        return CommUtil.binaryString2hexString(data);
    }

    //////////////////////////////////////////////////////////////////////////////
    ReceiveDataModel receiveDataModel = new ReceiveDataModel();

    public void getDio() {
//        int dio_1 = receiveDataModel.DIO_1;//实际通信值
        int dio_1 = 88;//测试数据
        Log.d("dio", "getDio_1: *************" + dio_1);

        for (int i = 4; i < 16; i++) {

            if (i == 4) firstAuxiliary = BitUtils.getBit_int(dio_1, i);
            if (i == 5) secondAuxiliary = BitUtils.getBit_int(dio_1, i);
            if (i == 6) thirdAuxiliary = BitUtils.getBit_int(dio_1, i);

            if (i == 7) firstASub = BitUtils.getBit_int(dio_1, i);
            if (i == 8) firstBSub = BitUtils.getBit_int(dio_1, i);
            if (i == 9) firstCSub = BitUtils.getBit_int(dio_1, i);

            if (i == 10) secondASub = BitUtils.getBit_int(dio_1, i);
            if (i == 11) secondBSub = BitUtils.getBit_int(dio_1, i);
            if (i == 13) secondCSub = BitUtils.getBit_int(dio_1, i);

            if (i == 14) thirdASub = BitUtils.getBit_int(dio_1, i);
            if (i == 15) thirdBSub = BitUtils.getBit_int(dio_1, i);

        }

//        int dio_2 = receiveDataModel.DIO_2;
        int dio_2 = 1;
        thirdCSub = BitUtils.getBit_int(dio_2, 0);

//        for (int i = 4; i < 16; i++) {
//
//            switch (i) {
//                case 4:
//                    firstAuxiliary = BitUtils.getBit_int(dio_1, i);
//                    break;
//                case 5:
//                    secondAuxiliary = BitUtils.getBit_int(dio_1, i);
//                    break;
//                case 6:
//                    thirdAuxiliary = BitUtils.getBit_int(dio_1, i);
//                    break;
//                case 7:
//                    firstASub = BitUtils.getBit_int(dio_1, i);
//                    break;
//                case 8:
//                    firstBSub = BitUtils.getBit_int(dio_1, i);
//                    break;
//                case 9:
//                    firstCSub = BitUtils.getBit_int(dio_1, i);
//                    break;
//                case 10:
//                    secondASub = BitUtils.getBit_int(dio_1, i);
//                    break;
//                case 11:
//                    secondBSub = BitUtils.getBit_int(dio_1, i);
//                    break;
//                case 12:
//                    firstCSub = BitUtils.getBit_int(dio_1, i);
//                    break;
//                case 13:
//                    secondCSub = BitUtils.getBit_int(dio_1, i);
//                    break;
//                case 14:
//                    thirdASub = BitUtils.getBit_int(dio_1, i);
//                    break;
//                case 15:
//                    thirdBSub = BitUtils.getBit_int(dio_1, i);
//                    break;
//            }
//
//        }

    }


    //电容开关的状态
    public List<Integer> getListRes() {
        getDio();//状态量赋值
//        mPassiveType = 3;//在这里拿到崔给的参数，赋值即可。！！！！！！！！！
//        mPassiveType = new WriteDataModel().passiveCapacitorGroup;
        mPassiveType = Constants.writeDataModel.passiveCapacitorGroup;
        List<Integer> list = new ArrayList<Integer>();
        if (isOpen) {
            list.add(R.drawable.capacitor_state_icon_close);//电容通断状态  FALSE为断， true为通
        } else {
            list.add(R.drawable.capacitor_state_icon_open);
        }

        if (firstAuxiliary == 0) {
            list.add(R.drawable.capacitor_state_auxiliary_open);//开关打开   白色  0
        } else {
            list.add(R.drawable.capacitor_state_auxiliary_close);//开关闭合  绿色  1
        }

        if (secondAuxiliary == 0) {
            list.add(R.drawable.capacitor_state_auxiliary_open);
        } else {
            list.add(R.drawable.capacitor_state_auxiliary_close);
        }

        if (thirdAuxiliary == 0) {
            list.add(R.drawable.capacitor_state_auxiliary_open);
        } else {
            list.add(R.drawable.capacitor_state_auxiliary_close);
        }
        //默认为  KVAR_300 ：对应3个共补，3个分补   6
        if (PassiveComponentType.getEnum(mPassiveType) == PassiveComponentType.KVAR_360) {//  对应4个共补   3个分补  7
            if (fourthAuxiliary == 0) {
                list.add(R.drawable.capacitor_state_auxiliary_open);
            } else {
                list.add(R.drawable.capacitor_state_auxiliary_close);
            }
        }

        list.add(getFirstSubRes());

        if (PassiveComponentType.getEnum(mPassiveType) != PassiveComponentType.KVAR_240) {// 对应3个共补，2个分补   5
            list.add(getSecondSubRes());
        }

        list.add(getThirdSubRes());//原来是这样
//        list.add(getFirstSubRes());//改成这样也行，那么这个的意义在哪里？
        return list;
    }

    private int getFirstSubRes() {
        int res = R.drawable.capacitor_state_abc_open;
        if (firstASub == 0 && firstBSub == 0 && firstCSub == 0) {
        } else if (firstASub == 0 && firstBSub == 0) {
            res = R.drawable.capacitor_state_abc_c;
        } else if (firstASub == 0 && firstCSub == 0) {
            res = R.drawable.capacitor_state_abc_b;
        } else if (firstBSub == 0 && firstCSub == 0) {
            res = R.drawable.capacitor_state_abc_a;
        } else if (firstASub == 0) {
            res = R.drawable.capacitor_state_abc_bc;
        } else if (firstBSub == 0) {
            res = R.drawable.capacitor_state_abc_ac;
        } else if (firstCSub == 0) {
            res = R.drawable.capacitor_state_abc_ab;
        } else {
            res = R.drawable.capacitor_state_abc_abc;
        }
        return res;
    }

    private int getSecondSubRes() {
        int res = R.drawable.capacitor_state_abc_open;
        if (secondASub == 0 && secondBSub == 0 && secondCSub == 0) {
        } else if (secondASub == 0 && secondBSub == 0) {
            res = R.drawable.capacitor_state_abc_c;
        } else if (secondASub == 0 && secondCSub == 0) {
            res = R.drawable.capacitor_state_abc_b;
        } else if (secondBSub == 0 && secondCSub == 0) {
            res = R.drawable.capacitor_state_abc_a;
        } else if (secondASub == 0) {
            res = R.drawable.capacitor_state_abc_bc;
        } else if (secondBSub == 0) {
            res = R.drawable.capacitor_state_abc_ac;
        } else if (secondCSub == 0) {
            res = R.drawable.capacitor_state_abc_ab;
        } else {
            res = R.drawable.capacitor_state_abc_abc;
        }
        return res;
    }

    private int getThirdSubRes() {
        int res = R.drawable.capacitor_state_abc_open;
        if (thirdASub == 0 && thirdBSub == 0 && thirdCSub == 0) {
        } else if (thirdASub == 0 && thirdBSub == 0) {
            res = R.drawable.capacitor_state_abc_c;
        } else if (thirdASub == 0 && thirdCSub == 0) {
            res = R.drawable.capacitor_state_abc_b;
        } else if (thirdBSub == 0 && thirdCSub == 0) {
            res = R.drawable.capacitor_state_abc_a;
        } else if (thirdASub == 0) {
            res = R.drawable.capacitor_state_abc_bc;
        } else if (thirdBSub == 0) {
            res = R.drawable.capacitor_state_abc_ac;
        } else if (thirdCSub == 0) {
            res = R.drawable.capacitor_state_abc_ab;
        } else {
            res = R.drawable.capacitor_state_abc_abc;
        }
        return res;
    }

}

